package com.clstu.tankgame5;

import java.io.*;
import java.util.Vector;

public class Recorder {
    private static int allEnemyTankNum = 0;
    private static BufferedReader br = null;
    private static BufferedWriter bw = null;
    private static String path = "src\\record.txt";
    private static Hero hero = null;
    private static Vector<EnemyTank> enemyTanks = null;

    public static Hero getHero() {
        return hero;
    }

    public static void setHero(Hero hero) {
        Recorder.hero = hero;
    }
    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }

    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }

    public static void addEnemyTankNum(){
        allEnemyTankNum++;
    }

    public static void keepRecord(){//此方法将成绩保存到文件中
        try {
            bw = new BufferedWriter(new FileWriter(path));
            bw.write(allEnemyTankNum+"\r\n");
            bw.write(hero.getX()+" "+hero.getY()+" "+hero.getDirect()+"\r\n");//写入hero的信息
            for (Tank t: enemyTanks ) {//写入敌人坦克的坐标和方向(活着的）
                if(t.isLive()) bw.write(t.getX()+" "+t.getY()+" "+t.getDirect()+"\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bw!=null) bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static Vector<EnemyTank> getLastInfo(){//返回上局游戏的进行情况
        try {
            br = new BufferedReader(new FileReader("src\\record.txt"));
            allEnemyTankNum = new Integer(br.readLine());//读入击杀坦克数
            String line = null;
            String[] split = null;
            if ((line=br.readLine())!=null) {
                split = line.split(" ");
                hero = new Hero(new Integer(split[0]),new Integer(split[1]),new Integer(split[2]));
            }
            enemyTanks = new Vector<>();
            while ((line=br.readLine())!=null){
                split = line.split(" ");
                enemyTanks.add(new EnemyTank(new Integer(split[0]),new Integer(split[1]),new Integer(split[2])));
            }
        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(br != null) br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return enemyTanks;
    }

}
